package cn.wolfcode.web.controller;


import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.domain.Exam;
import cn.wolfcode.domain.ExamQuestion;
import cn.wolfcode.domain.Question;
import cn.wolfcode.domain.QuestionItem;
import cn.wolfcode.service.IExamService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.PermissionAnnotation;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping("/exam")
public class ExamController {

    @Autowired
    private IExamService examService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Exam> pageInfo = examService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "exam/newTestPaper";
    }


    @RequestMapping("/go")
    public String saveOrUpdate(Model model,Exam exam,Integer one,Integer many,Integer choice,Integer[] score) {
        examService.save(exam);
        List<Question> list = examService.getList(1);
        ArrayList<Long> ids = new ArrayList<>();
        for (Question question : list) {
            ids.add(question.getId());
        }

        for (int i2 =0;i2<one;i2++){
            int i = new Random().nextInt(ids.size());
            Question question = list.get(i);
            ids.remove(i);
            ExamQuestion examQuestion = new ExamQuestion();
            examQuestion.setExamId(exam.getId());
            examQuestion.setQuestionId(question.getId());
            examQuestion.setSequence(i2+1);
            examQuestion.setScore(score[0]);
            examService.insert(examQuestion);
        }

        List<Question> list2 = examService.getList(2);
        ArrayList<Long> ids2 = new ArrayList<>();
        for (Question question : list2) {
            ids2.add(question.getId());
        }

        for (int i2 =0;i2<many;i2++){
            int i = new Random().nextInt(ids2.size());
            Question question = list2.get(i);
            ids2.remove(i);
            ExamQuestion examQuestion = new ExamQuestion();
            examQuestion.setExamId(exam.getId());
            examQuestion.setQuestionId(question.getId());
            examQuestion.setSequence(i2+1);
            examQuestion.setScore(score[1]);
            examService.insert(examQuestion);
        }

        List<Question> list3 = examService.getList(3);
        ArrayList<Long> ids3 = new ArrayList<>();
        for (Question question : list3) {
            ids3.add(question.getId());
        }

        for (int i2 =0;i2<choice;i2++){
            int i = new Random().nextInt(ids3.size());
            Question question = list3.get(i);
            ids3.remove(i);
            ExamQuestion examQuestion = new ExamQuestion();
            examQuestion.setExamId(exam.getId());
            examQuestion.setQuestionId(question.getId());
            examQuestion.setSequence(i2+1);
            examQuestion.setScore(score[2]);
            examService.insert(examQuestion);
        }

        model.addAttribute("exam",exam);

        //设置题目 和 选项 以及答案
        List<Question> question = examService.getQuestion(exam.getId(), 1);
        List<Question> question2 = examService.getQuestion(exam.getId(), 2);
        List<Question> question3 = examService.getQuestion(exam.getId(), 3);
        model.addAttribute("one",question);
        model.addAttribute("many",question2);
        model.addAttribute("choice",question3);

        //设置总分
        ExamQuestion count1 = examService.getCount(exam.getId(), 1);
        ExamQuestion count2 = examService.getCount(exam.getId(), 2);
        ExamQuestion count3 = examService.getCount(exam.getId(), 3);

        model.addAttribute("oneCount",count1.getCount()*count1.getScore());
        model.addAttribute("manyCount",count2.getCount()*count2.getScore());
        model.addAttribute("choiceCount",count3.getCount()*count3.getScore());

        return "exam/testPaper";
    }



}
